<?php

namespace App\Models\Member;

use App\Models\BaseModel;
use App\Common\ValidateHandler;
use Config, DB, Auth;

class BankLib extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'user_id', 'bank_name', 'bank_code', 'deposit', 
        'accepter_name', 'accepter_phone', 'created_at','updated_at'
    ];
    // 根据银行卡ID 获取信息 - 如果为0 或 获取不到 返回空数据
    public function bankInfo($bankId)
    {
        $bank = $bankId == 0 ? null : $this->where('id', $bankId)->first();
        if ($bank) {
            return $this->successClient($bank, '获取银行卡信息!');
        } else {
            return $this->errorClient($bank, '获取银行卡信息失败!');
        }
    }
    // 根据用户ID 获取银行卡列表 - 传递默认银行卡, 如无默认则为0
    public function lists($id, $bankId = 0)
    {
        $lists = $this->where('user_id', $id)->get()->toArray();
        if ($lists || $lists == []) {
            return $this->successClient(['lists' => $lists, 'banId' => $bankId], '获取银行卡列表');
        } else {
            return $this->errorClient($lists, '获取银行卡列表失败!'); 
        }
    }

    // 插入银行卡数据
    public function insertBank($data)
    {
        $bank = [];
        $bank['store_id'] = $data['store_id'];
        $bank['user_id'] = $data['user_id'];
        $bank['bank_name'] = $data['bank_name'];
        $bank['bank_code'] = $data['bank_code'];
        $bank['deposit'] = isset($data['deposit']) ? $data['deposit'] : '';
        $bank['accepter_name'] = $data['accepter_name'];
        $bank['accepter_phone'] = $data['accepter_phone'];
        $bank['created_at'] = date('Y-m-d H:i:s');
        $validate = $this->validate($bank);
        if (!$validate['res']) {
            return $this->errorClient($validate, $validate['info']);
        }
        DB::beginTransaction();
        try {
            $insert = $this->insertGetId($bank);
            if (!$insert) {
                return $this->errorClient($insert, '插入银行卡失败!'); 
            }
            if ($data['isDefault'] == 1) {
                $user_up = User::where('id', $data['user_id'])->update(['bank_id' => $insert]);
                if (!$user_up) {
                    return $this->errorClient($bank, '默认银行卡更新失败!'); 
                }
            }
            DB::commit();
            return $this->successClient($insert, '插入银行卡成功');
        } catch (\Exception $e) {
            DB::rollBack();
            return $e->getLine().$e->getMessage();
        }
    }
    public function updateBank($data)
    {
        $bankId = $data['bank_id'];
        unset($data['bank_id']);
        $bank = [];
        $bank['bank_name'] = $data['bank_name'];
        $bank['bank_code'] = $data['bank_code'];
        $bank['deposit'] = isset($data['deposit']) ? $data['deposit'] : '';
        $bank['accepter_name'] = $data['accepter_name'];
        $bank['accepter_phone'] = $data['accepter_phone'];
        $validate = $this->validate($bank);
        if (!$validate['res']) {
            return $this->errorClient($validate, $validate['info']);
        }
        DB::beginTransaction();
        try {
            $update = $this->where('id', $data['id'])->where('user_id', $data['user_id'])->update($bank);
            if (!$update) {
                return $this->errorClient($update, '更新银行卡失败!'); 
                
            }
            // 是默认地址 且 与当前默认地址不一致 , 更新默认地址
            if ($data['isDefault'] == 1 && $bankId != $data['id']) {
                $user_up = User::where('id', $data['user_id'])->update(['bank_id' => $data['id']]);
                if (!$user_up) {
                    return $this->errorClient($data, '默认银行卡更新失败!'); 
                }
            }
            // 不是默认地址 且 与当前默认地址一致 , 更默认地址为空
            if ($data['isDefault'] == 0 && $bankId == $data['id']) {
                $user_up = User::where('id', $data['user_id'])->update(['bank_id' => 0]);
                if (!$user_up) {
                    return $this->errorClient($data, '默认银行卡取消失败!'); 
                }
            }
            DB::commit();
            return $this->successClient($update, '更新银行卡成功');
        } catch (\Exception $e) {
            DB::rollBack();
            return $e->getLine().$e->getMessage();
        }
    }

    // 默认地址id 默认为0
    public function delBank($id, $userId, $bankId = 0)
    {
        DB::beginTransaction();
        try {
            $del = $this->where('id', $id)->where('user_id', $userId)->delete();
            if (!$del) {
                return $this->errorClient($del, '删除银行卡失败!请刷新重试!'); 
            }
            // 如果删除的地址是默认地址 => ID = 默认地址id 更新默认地址
            if ($id == $bankId) {
                $default = User::where('id', $userId)->update(['bank_id' => 0]);
                if (!$default) {
                    return $this->errorClient($id, '当前为默认银行卡, 默认银行卡更新失败!'); 
                }
            }
            DB::commit();
            return $this->successClient($del, '删除银行卡成功');
        } catch (\Exception $e) {
            DB::rollBack();
            return $e->getLine().$e->getMessage();
        }
    }

    // 验证银行卡列表的
    private function validate($data)
    {
        $arr = [
            0 => [
                'name' => '银行卡号',
                'value' => $data['bank_code'],
                'func' => 'verification_bank_card'
            ],
            2 => [
                'name' => '持卡人名字',
                'value' => $data['accepter_name'],
                'func' => 'is_all_chinese'
            ],
            3 => [
                'name' => '手机号',
                'value' => $data['accepter_phone'],
                'func' => 'is_phone'
            ],
            4 => [
                'name' => '银行名字',
                'value' => $data['bank_name'],
                'func' => 'is_all_chinese'
            ],
            5 => [
                'name' => '开户行',
                'value' => $data['deposit'],
                'func' => 'is_empty'
            ]
        ];
        return ValidateHandler::do($arr);
    }
}
